package com.sunxiansheng.user.controller;

import com.sunxiansheng.response.Result;
import com.sunxiansheng.user.entity.PageResult;
import com.sunxiansheng.user.entity.dto.UserDto;
import com.sunxiansheng.user.entity.po.UserPo;
import com.sunxiansheng.user.entity.req.UserListReq;
import com.sunxiansheng.user.entity.req.UserReq;
import com.sunxiansheng.user.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Description:
 * @Author sun
 * @Create 2024/7/8 17:55
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping
    public Integer addUser(@RequestBody UserReq userReq) {
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(userReq, userDto);
        int i = userService.addUser(userDto);
        return i;
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        userService.delete(id);
        return Result.ok();
    }

    @GetMapping
    public Result<PageResult<UserPo>> getPage(@RequestBody UserListReq userListReq) {
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(userListReq, userDto);
        PageResult<UserPo> userPage = userService.getUserPage(userDto);
        return Result.ok(userPage);
    }

}